package com.example.academic_affairs_system.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

/**
 * Mybatis-plus 数据处理配置
 *
 * @author hjx
 * @since 2023/01/06 8:10 PM
 */
@Configuration
public class MybatisPlusMetaObjectHandlerConfig implements MetaObjectHandler {
  @Override
  public void insertFill(MetaObject metaObject) {
    this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
  }

  @Override
  public void updateFill(MetaObject metaObject) {
    this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
  }

  @Bean
  public MybatisPlusInterceptor paginationInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
  }
  /*  @Bean
  public MybatisPlusPropertiesCustomizer plusPropertiesCustomizer() {
    return plusProperties ->
        plusProperties
            .getGlobalConfig()
            .getDbConfig()
            .setKeyGenerators(Collections.singletonList(new H2KeyGenerator()));
  }*/
}
